package com.test.servlet;

import com.test.model.Employee;
import com.test.service.EmployeeService;
import com.test.service.EmployeeServiceImpl;
import com.test.utils.TranscodeUtil;
import com.test.utils.UUIDUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;


public class EmployeeServlet extends HttpServlet {
    EmployeeService employeeService  = new EmployeeServiceImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询list数据
        try {
           String method = request.getParameter("method");
           if("list".equals(method)){
               //执行查询所有数据方法
               list(request,response);
           }else if("toAddEmployeePage".equals(method)){
               toAddEmployeePage(request,response);
           }else if("add".equals(method)){
               //新增员工
               add(request,response);
           }else{
               list(request,response);
           }

        }catch (Exception  e){
            request.getRequestDispatcher("error.jsp").forward(request,response);
            e.printStackTrace();
        }



    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    /**
     * 查询所有数据 返回list
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.拿到了查询条件
        String  name =  request.getParameter("name");
        String  salary =  request.getParameter("salary");
        String  salary2 =  request.getParameter("salary2");
        if(salary!=null&&salary2!=null&&!"".equals(salary)&&!"".equals(salary2)){
            if(Integer.parseInt(salary)>Integer.parseInt(salary2)){
                request.setAttribute("msg","薪资开始值不能大于结束值");
                request.getRequestDispatcher("error.jsp").forward(request,response);
            }
        }
        request.setCharacterEncoding("UTF-8");
           /* if(null != name){
                name  =  new String(name.getBytes("iso8859-1"),"utf-8");
            }*/
        name = TranscodeUtil.transcode(name);
        System.out.println("name="+name);
        System.out.println("salary="+salary);
        System.out.println("salary2="+salary2);
        //2.执行 查询   去请求service的查询list的selctEmployeeList方法   由service去请求dao  由dao执行sql 将返回 结果给service-->servelt  List
        //定义，申明   实例化
        List<Employee>  employeeList = employeeService.selectEmployeeList(name,salary,salary2);
        //return employeeList;
        //1.request域
        //2.session域
        //将查询来的结果集放到request作用域里面
        request.setAttribute("employees",employeeList);
        //请求的queryList.jsp携带原来的 request,response

        request.getRequestDispatcher("queryList.jsp").forward(request,response);
    }

    /**
     * 去新增
     * @param request
     * @param response
     * @throws Exception
     */
    protected void toAddEmployeePage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.getRequestDispatcher("addEmp.jsp").forward(request,response);

    }

    /**
     * 新增
     * @param request
     * @param response
     * @throws Exception
     */
    protected void add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //name gender birthday email salary remark
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //第一步：获取页面提交过来的参数 以及处理乱码
       String  name = request.getParameter("name");
      String gender =  request.getParameter("gender");
      String birthday =  request.getParameter("birthday");
      String email =  request.getParameter("email");
      String salary =  request.getParameter("salary");
      String remark =  request.getParameter("remark");
        Employee employee = new Employee();
        employee.setId(UUIDUtil.getUUID());
        employee.setName(TranscodeUtil.transcode(name));
        employee.setGender(TranscodeUtil.transcode(gender));
        employee.setBirthday(sdf.parse(birthday));
        employee.setEmail(email);
        employee.setSalary(salary);
        employee.setRemark(TranscodeUtil.transcode(remark));
      //第二步：请求service
     int res = employeeService.add(employee);
     if(res > 0){
         //第三步：返回list页面 查看是否新增成功  重定向
         response.sendRedirect("/employee?method=list");
     }else{
         request.getRequestDispatcher("error.jsp").forward(request,response);
     }



    }


}
